package com.pv.bfc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.JstlView;

import com.pv.bfc.dao.FolderRepository;
import com.pv.bfc.dao.UserRepository;
import com.pv.bfc.model.BriefcaseUser;
import com.pv.bfc.model.Folder;
import com.pv.bfc.utils.GetUserDetailsFromContext;

/**
 * Retrieves the list of user folders to render on the folders page. When no
 * folders are found, the user is forwarded to a page that holds a form which
 * can be used to create a new document folder.
 * 
 */
public class FoldersController extends AbstractController {

	/**
	 * The view to forward to in case an folder needs to be created.
	 */
	private static final String CREATE_VIEW = "forward:createfolder.htm";

	/**
	 * The view to forward to in case an folder already created.
	 */
	// private static final String DISPLAY_VIEW = "forward:viewfolders.htm";
	/**
	 * The model key used to retrieve the message from the model.
	 */
	private static final String MODEL_KEY = "message";

	private static final String USERNAME_KEY = "username";
	/**
	 * The unique key for retrieving the text associated with this message.
	 */
	private static final String MSG_CODE = "message.create.folder";

	private FolderRepository folderRepo;

	private UserRepository userRepo;

	private GetUserDetailsFromContext usrDtls;

	private static final Log logger = LogFactory
			.getLog(FoldersController.class);

	public FoldersController() {
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// Getting User Details
		User principal = (User) usrDtls.getPrincipal();

		String userName = principal.getUsername();
		logger.info("Username " + userName);

		/*
		 * HttpSession session = request.getSession(); SecurityContext ctx2 =
		 * (SecurityContext) session .getAttribute("SPRING_SECURITY_CONTEXT");
		 * Authentication auth2 = ctx2.getAuthentication();
		 */

		// create a new session
		HttpSession session = request.getSession();

		// put the UserDetails object here.
		// session.setAttribute("userDetails", principal);

		// Getting the User Info based on Username
		BriefcaseUser user = userRepo.retreiveUserByUserName(userName);

		// put the User First Name here object here.
		session.setAttribute("firstname", user.getFirstName());

		// / List<Folder> folders = folderRepo.retrieveAllFolders();

		// Getting list of Folders beloning to a User
		List<Folder> folders = folderRepo.retrieveUserFolders(user);

		if (folders == null || folders.isEmpty()) {
			ModelAndView mav = new ModelAndView(CREATE_VIEW);
			mav.addObject(MODEL_KEY, MSG_CODE);
			mav.addObject(USERNAME_KEY, userName);
			return mav;
		} else {
			/*
			 * ModelAndView mav = new ModelAndView(DISPLAY_VIEW); return
			 * mav.addObject(folders);
			 */

			/*
			 * Iterator<Folder> it = folders.iterator();
			 * 
			 * while (it.hasNext()) { Folder fol = it.next(); }
			 */

			Map<String, List<Folder>> model = new HashMap<String, List<Folder>>();
			model.put("folder", folders);
			JstlView view = new JstlView();
			view.setUrl("/WEB-INF/jsp/folderlist.jsp");
			return new ModelAndView("folderlist", model);

		}
	}

	public void setFolderRepo(FolderRepository folderRepo) {
		this.folderRepo = folderRepo;
	}

	public void setUserRepo(UserRepository userRepo) {
		this.userRepo = userRepo;

	}

	public void setusrDtls(GetUserDetailsFromContext usrDtls) {
		this.usrDtls = usrDtls;
	}
}
